//
//  DetailsViewController.m
//  MY Project For Sport
//
//  Created by dllo on 16/1/28.
//  Copyright © 2016年 dilo. All rights reserved.
//

#import "DetailsViewController.h"
#import "LTView.h"
#import "NetworkHandle.h"
#import "ModelForDetails.h"
#import "NetworkingHandlerOfBlock.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "CellForDetali.h"
#import "ModelForDetailsNews.h"
#import "WebviewForNews.h"
#import "UIColor+Wonderful.h"

#define WIDTH self.view.frame.size.width
#define HEIGTH self.view.frame.size.height

#define WEIGHTT [UIScreen mainScreen].bounds.size.width / 375
#define HEIGHTT [UIScreen mainScreen].bounds.size.height / 667



@interface DetailsViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UIWebView *webView;
@property (nonatomic, retain) ModelForDetails *model;
@property (nonatomic, retain) LTView *backGroundView;
@property (nonatomic, retain) NSString *strUrl;
@property (nonatomic, retain) NSString *webUrl;
@property (nonatomic, retain) NSMutableArray *array;

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) UIView *viewCustom;

@property (nonatomic, retain) UIImageView *team1;
@property (nonatomic, retain) UIImageView *team2;

@property (nonatomic, retain) UILabel *team1Label;
@property (nonatomic, retain) UILabel *team2Label;

@property (nonatomic, retain) UILabel *signLabel;



@end

@implementation DetailsViewController
-(void)dealloc {
    
    [_model release];
    [_webView release];
    [_backGroundView release];
    [_strUrl release];
    [_webUrl release];
    [_tableView release];
    [_ID release];
    [_array release];
    [_viewCustom release];
    [_team1 release];
    [_team2 release];
    [_team1Label release];
    [_team2Label release];
    [_signLabel release];
    [_urling release];
    [super dealloc];
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
  //  self.navigationController.navigationBarHidden = YES;
    
    [self createTableView];
    [self createKVO];
    [self handleNewsData];
   // [self createCustom];
}

- (void)lastAction:(UIButton *) button {
    
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.alpha = 1;
    [self.navigationController popToRootViewControllerAnimated:NO];
    
}


- (void)handleNewsData {
    
    NSString *str = @"http://platform.sina.com.cn/sports_client/news?app_key=2586208540&_version__=3.2.1.01&__os__=android&match_id=";
    NSString *str2 = @"&zt=1&news_type=2,3&level=2,3&len=20&client_deviceid=7005145ee5bbe7f1f97c5e227f4ab771";
    NSString *newsStr = [str stringByAppendingFormat:@"%@%@", self.ID, str2];
  //  NSLog(@"%@", newsStr);
    [NetworkingHandler networkingWithUrl:newsStr completeHandle:^(NSURLResponse *response, NSData *data, id result) {
       // NSLog(@"%@", newsStr);
        self.array = [NSMutableArray array];
        
        NSDictionary *dic = [result objectForKey:@"result"];
        
        NSArray *array = [dic objectForKey:@"data"];
        
        for (NSDictionary *dict in array) {
            
            ModelForDetailsNews *model = [[ModelForDetailsNews alloc]init];
            
            [model setValuesForKeysWithDictionary:dict];
            [self.array addObject:model];
            [model release];
            
        }
        
        [self.tableView reloadData];
        
    }];
    
}

/**监听 tableView 的偏移量 */
- (void)createKVO {
    
    [self.tableView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
    
    
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    
    
    CGFloat y = [[change objectForKey:@"new"] CGPointValue].y;
  
    if ( y > 0 ) {
        
        
        // navigation通过 tableview 的偏移量逐渐隐藏
        self.navigationController.navigationBar.alpha = 1 - y / (self.view.frame.size.height /5 * 2 - 64);
        self.backGroundView.myView.alpha = 1 - y / (self.view.frame.size.height /5 * 2 - 64);
        self.backGroundView.dress1.alpha = 1 - y / (self.view.frame.size.height /5 * 2 - 64);
        self.backGroundView.dress2.alpha = 1 - y / (self.view.frame.size.height /5 * 2 - 64);
        
    // 自定义的头部视图根据 tableview 的偏移量逐渐显示
        
        self.viewCustom.alpha = (y - 50) / (self.view.frame.size.height /5 * 2 - 64);
        
    }
    
}



- (void)createTableView {
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    [self handleData];
    
    [self.tableView registerClass:[CellForDetali class] forCellReuseIdentifier:@"pool"];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CellForDetali *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    
    ModelForDetailsNews *model = [self.array objectAtIndex:indexPath.row];
    
    [cell passModelForDetali:model];
    
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return self.view.frame.size.height / 7;
    
    
}

/**数据处理以及给 myview 赋值 */
- (void)handleData {
    
    [NetworkingHandler networkingWithUrl:self.urling completeHandle:^(NSURLResponse *response, NSData *data, id result) {
        NSLog(@"%@", self.urling);
        NSDictionary *dic = [result objectForKey:@"result"];
        NSDictionary *dict = [dic objectForKey:@"data"];
        
        self.model = [[ModelForDetails alloc] init];
        [self.model setValuesForKeysWithDictionary:dict];
        
        
        self.backGroundView = [[LTView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height /5 * 2)];
        
        if ([self.model.MatchId isEqualToString:@"0"]) {
            
            NSString *str1 = @"http://client.mix.sina.com.cn/startrek/api/wap/get_vote/?match_id=";
            NSString *str2 = self.model.livecast_id;
            NSString *str3 = @"&app_key=2586208540";
            
            self.strUrl = [str1 stringByAppendingFormat:@"%@%@", str2,str3];
            
        }else {
            
            NSString *str1 = @"http://client.mix.sina.com.cn/startrek/api/wap/get_vote/?match_id=";
            NSString *str2 = self.model.MatchId.mutableCopy;
            NSString *str3 = @"&app_key=2586208540";
            self.strUrl = [str1 stringByAppendingFormat:@"%@%@", str2,str3];
        }
        
        [_model release];
        
        [NetworkingHandler networkingWithUrl:self.strUrl completeHandle:^(NSURLResponse *response, NSData *data, id result) {
            
            NSDictionary *dic = [result objectForKey:@"result"];
            NSDictionary *dict = [dic objectForKey:@"data"];
            
            [self.model setValuesForKeysWithDictionary:dict];
            
            
            self.backGroundView.team1Number.text = self.model.host;
            self.backGroundView.team2Number.text = self.model.visit;
            
            self.backGroundView.SliderOfLike.minimumValue = 0;
            self.backGroundView.SliderOfLike.maximumValue = 1;
            self.backGroundView.SliderOfLike.minimumTrackTintColor = [UIColor redColor];
            self.backGroundView.SliderOfLike.maximumTrackTintColor = [UIColor blueColor];
            self.backGroundView.SliderOfLike.value = [self.model.host floatValue] / ([self.model.host floatValue] + [self.model.visit floatValue]);
            
            self.backGroundView.userInteractionEnabled = NO;
            
            
            
        }];
        
        [self.backGroundView.myView sd_setImageWithURL:[NSURL URLWithString:self.model.Flag2]];
        self.backGroundView.matchOfTitle.text = self.model.Title;
        [self.backGroundView.team1Image sd_setImageWithURL:[NSURL URLWithString:self.model.Flag1]];
        [self.backGroundView.team2Image sd_setImageWithURL:[NSURL URLWithString:self.model.Flag2]];
        self.backGroundView.team1.text = self.model.Team1;
        self.backGroundView.team2.text = self.model.Team2;
        self.backGroundView.team1Score.text = self.model.Score1;
        self.backGroundView.team2Score.text = self.model.Score2;
        [self.backGroundView.dress1 sd_setImageWithURL:[NSURL URLWithString:self.model.Dress1]];
        [self.backGroundView.dress2 sd_setImageWithURL:[NSURL URLWithString:self.model.Dress2]];
        self.backGroundView.matchName.text = self.model.LeagueType_cn;
        self.backGroundView.roundNumber.text = self.model.Round_cn;
        self.backGroundView.type1Score.text = self.model.penalty1;
        self.backGroundView.type2Score.text = self.model.penalty2;
        self.backGroundView.typeOfEnd.text = self.model.LiveStatusExtra_cn;
        
        self.tableView.tableHeaderView = self.backGroundView;
        
        [_backGroundView release];
        
        
        self.viewCustom = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 64)];
        [self.view addSubview:self.viewCustom];
        [_viewCustom release];
        
        self.viewCustom.backgroundColor = [UIColor deepSkyBlue];
        self.viewCustom.alpha = 0;
        
        self.team1 = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH / 11, 5, 50, 50)];
        [self.viewCustom addSubview:self.team1];
        [_team1 release];
        [self.team1 sd_setImageWithURL:[NSURL URLWithString:self.model.Flag1]];
        
        self.team1Label = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 11 + 50 + WIDTH / 11, 5, 50, 50)];
        [self.viewCustom addSubview:self.team1Label];
        [_team1Label release];
        self.team1Label.textAlignment = NSTextAlignmentCenter;
        self.team1Label.text = self.model.Score1;
        
        
        
        self.signLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
        [self.viewCustom addSubview:self.signLabel];
        [_signLabel release];
        self.signLabel.textAlignment = NSTextAlignmentCenter;
        self.signLabel.center = self.viewCustom.center;
        self.signLabel.text = @":";
        
        self.team2 = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH - WIDTH / 11 - 50, 5, 50, 50)];
        [self.viewCustom addSubview:self.team2];
        [_team2 release];
        
        [self.team2 sd_setImageWithURL:[NSURL URLWithString:self.model.Flag2]];
        
        
        self.team2Label = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH - WIDTH / 11 - 50 - 50 - WIDTH / 11 , 5, 50, 50)];
        [self.viewCustom addSubview:self.team2Label];
        [_team2Label release];
        self.team2Label.textAlignment = NSTextAlignmentCenter;
        self.team2Label.text = self.model.Score2;
        
        
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(5, 20, 30, 30);
        [self.view addSubview:button];
        
        //  button.backgroundColor = [UIColor redColor];
        [button setImage:[UIImage imageNamed:@"iconfont-zuojiantou"] forState:UIControlStateNormal];
        
        [button addTarget:self action:@selector(lastAction:) forControlEvents:UIControlEventTouchUpInside];
        
    }];
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
   
    WebviewForNews *web = [[WebviewForNews alloc] init];
    
    ModelForDetailsNews *model = [self.array objectAtIndex:indexPath.row];
    
    web.url = model.url;
    
    [self.navigationController pushViewController:web animated:NO];
    
    [web release];
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
